<?php

/**
 * Framework_Module_CP
 *
 * @author      Ian Szewczyk <roamzero@gmail.com>
 * @copyright   (c) 2007 Ian Szewczyk. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package Modules
 * @filesource
 */

/**
 * Framework_Module_CP
 *
 * @author Ian Szewczyk <roamzero@gmail.com>
 * @package Modules
 */
class Framework_Module_CP extends Framework_Module
{

	/**
     * $owner
     *
     * @access      public
     * @var         object		$owner      Instance of Framework_Owner
     */
	protected $owner;

	
	/**
     * __construct
     *
     * Initializes the module: sets the default presenter, the allowed
     * conrollers, and the allowed events
     *
     * @access 		public
     * @return 		void
     */
	public function __construct()
	{
		parent::__construct();
		$this->authLayer = 'CP';
		$this->presenter = 'Smarty';
		if (Framework::$request->isAjax()) {
			$this->pageTemplateFile = 'CpPageAjax.tpl';
		} else {
		 	$this->pageTemplateFile = 'CpPage.tpl';
			
		}
		$this->tplFile = 'Default.tpl';
		$this->controllers[] = 'Peopletab';
		$this->owner = Framework_Owner::singleton();



		$navigator = new Framework_Navigator('/'.Framework::$request->ownerName, 'cpNav');
		$navigator->setItem('Settings', '/CP/Settings', 'Site Settings');
		$navigator->setItem('Skins', '/CP/Skins', 'Look and Feel');
		$navigator->setItem('Messages', '/CP/Messages', 'My Messages');

		$meta = Framework_Meta::factory();

		$metaTable = $meta->getTable();

		$queryArray = array(
			'sqlKey' => array(
				'select' => 'idMetaModule, moduleSettingsName',
				'where'  => 'moduleSettingsCP = 1'
			)
		);

		$result = $metaTable->select($queryArray);

		foreach ($result AS $row) {
			$navigator->setItem(ucfirst($row['moduleSettingsName']), '/'.$row['idMetaModule'].'/CP');
		}

		$this->setData('cpNav', $navigator->getNavigation());


	}

	/**
     * __default
     *
     * Loads general info (unread private messages, users browsing, etc)
     *
     * @access 		public
     * @return 		void
     */
	public function __default()
	{

		$messageDB = Framework_DB::dbFactory('Message');
			
		$unread = $messageDB->selectCount(array(
			'sqlKey' => 'getUnreadMessages',
			'params' => array($this->owner->idSiteUser)
		));
			
		if (!PEAR::isError($unread)) {
			$this->setData('unread',$unread);
		}
			
			
		$last15 = $this->owner->getVisitHistory();
		if (!PEAR::isError($last15)) {
			$this->setData('last15',$last15);
		}


		$currentlyBrowsing = $this->owner->getCurrentlyBrowsing();
		if (!PEAR::isError($currentlyBrowsing)) {
			$this->setData('currentlyBrowsing',$currentlyBrowsing);
		}


	}





}


?>